package com.etc.controller;

import com.etc.entity.Advert;
import com.etc.entity.Notice;
import com.etc.service.AdvertService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.io.FileUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/advert")
public class AdvertController {
    @Autowired
    private AdvertService advertService;

    @RequestMapping("/add.do")
    public String advertAdd(@Param("file")MultipartFile file, @Param("advert_name")String advert_name, @Param("advert_link")String advert_link) throws IOException {
        String advert_image;
        String rootPath=NoticeController.class.getResource("").getPath().substring(0,NoticeController.class.getResource("").getPath().length()-46);
        File file1 = new File(rootPath+"src/main/webapp/static/image/" + file.getOriginalFilename());
        if(!file1.exists())
        {
            try {
                file1.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        FileUtils.copyToFile(file.getInputStream(),file1);
        advert_image="/static/image/"+file.getOriginalFilename();
        advertService.advertAdd(advert_name,advert_image,advert_link);
        return "redirect:/advert/queryAll.do/1";
    }
    @RequestMapping("/delete.do")
    public String advertDelete(int id){
        advertService.advertDelete(id);
        return "redirect:/advert/queryAll.do/1";
    }
    @RequestMapping("/query1.do")
    @ResponseBody
    public Advert advertQueryById(@RequestBody Advert advert){
        Advert advert1 = advertService.advertQueryById(advert.getAdvert_id());
        return advert1;
    }
    @RequestMapping("/queryAll.do")
    public String queryAll(Model model){
        List<Advert> adverts = advertService.queryAll();
        int pagesize=5;
        int counttotal=advertService.queryAll().size();
        int pages=1;
        if(counttotal/pagesize==0){
            pages=1;
        }else {
            pages=(counttotal-1)/pagesize+1;
        }
        model.addAttribute("adverts",adverts);
        model.addAttribute("pages",pages);
        model.addAttribute("pagecurrent",1);
        model.addAttribute("counttotal",counttotal);
        model.addAttribute("pagesize",pagesize);
        return "Advert";
    }
    @RequestMapping("/modify.do")
    public String modify(MultipartFile file,@Param("advert_name")String advert_name,@Param("advert_link")String advert_link,@Param("advert_id")int advert_id) throws IOException {
        String advert_image;
        if (file.isEmpty()) {
            advert_image=null;
        }else {
            String rootPath=NoticeController.class.getResource("").getPath().substring(0,NoticeController.class.getResource("").getPath().length()-46);
            File file1 = new File(rootPath+"src/main/webapp/static/image/" + file.getOriginalFilename());
            if(!file1.exists())
            {
                try {
                    file1.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            FileUtils.copyToFile(file.getInputStream(),file1);
            advert_image="/static/image/"+file.getOriginalFilename();
        }
        advertService.modify(advert_name,advert_image,advert_link,advert_id);
        return "redirect:/advert/queryAll.do/1";
    }
    @RequestMapping("/queryByName.do")
    public String queryByTitle(String advert_name, Model model){
        List<Advert> adverts = advertService.queryByName(advert_name);
        int pagesize=5;
        int counttotal=advertService.queryByName(advert_name).size();
        int pages=1;
        if(counttotal/pagesize==0){
            pages=1;
        }else {
            pages=(counttotal-1)/pagesize+1;
        }
        model.addAttribute("adverts",adverts);
        model.addAttribute("pages",pages);
        model.addAttribute("pagecurrent",1);
        model.addAttribute("counttotal",counttotal);
        model.addAttribute("pagesize",pagesize);
        return "Advert";
    }
    @RequestMapping(value = "/queryAll.do/{pagenum}",method = RequestMethod.GET)
    public String advertsmanager(Model model, @PathVariable(value = "pagenum",required = false) String pagenum){
        System.out.println("进入方法"+pagenum);
        //每页长度
        int pagesize=5;
        //当前页
        int pagecurrent=1;
        //查询数据库总数据
        int counttotal=advertService.queryAll().size();
        //判断总页数
        int pages=1;
        if(counttotal/pagesize==0){
            pages=1;
        }else {
            pages=(counttotal-1)/pagesize+1;
        }
        //判断当前页
        if(pagenum==null){
            pagecurrent=1;
        }else {
            pagecurrent=Integer.valueOf(pagenum);
        }
        //首页每页判断
        if(pagecurrent<=1){
            pagecurrent=1;
        }else if(pagecurrent>=pages){
            pagecurrent=pages;
        }
        //分页start
        int pagestart=0;
        pagestart=(pagecurrent-1)*pagesize;




        //根据当前页和页面尺度进行分页
        PageHelper.startPage(pagecurrent,pagesize);
        List<Advert> adverts = advertService.queryAll();
        //加入公告列表信息
        model.addAttribute("adverts",adverts);
        //加入总页数
        model.addAttribute("pages",pages);
        //加入当前页
        model.addAttribute("pagecurrent",pagecurrent);
        //加入查询总条数
        model.addAttribute("counttotal",counttotal);
        //加入每页宽度
        model.addAttribute("pagesize",pagesize);
        //返回页面
        return "Advert";
    }
}
